//
//  KKOrderDetailViewModel.m
//  FreightAssistant
//
//  Created by Mac mini on 15/12/31.
//  Copyright © 2015年 leo. All rights reserved.
//

#import "KKSupplyOrderDetailViewModel.h"

@implementation KKSupplyOrderDetailViewModel

- (instancetype)initWithModel:(KKBaseItem *)itemModel tableView:(__weak UITableView *)tableView {
    self = [super init];
    if (self) {
        
        _orderItem = (KKSupplyOrderItem*)itemModel;
        _tableView = tableView;
        
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return self;
}

#pragma mark - UITableView methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 1) {
        return _orderItem.kk_suppplyCommodityItems.count;
    }
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 5.0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSInteger section = indexPath.section;
    
    OrderDetailTableViewCell *cell;
    
    switch (section) {
        case 0:
        {
            cell = [OrderDetailTableViewCell cellFromTableView: tableView index: 1];
            
            KKCustomerItem *customer = [_orderItem kk_customerItem];
            
            cell.kk_customerNameLabel.text = customer.kk_name;
            cell.kk_customerAddressLabel.text = [NSString stringWithFormat: @"收货地址：%@", customer.kk_address];
            cell.kk_customerPhoneLabel.text = customer.kk_phone;
        }
            break;
        case 1:
        {
            cell = [OrderDetailTableViewCell cellFromTableView: tableView index: 0];
            
            KKSupplyCommodityItem *item = [_orderItem.kk_suppplyCommodityItems objectAtIndex: indexPath.row];
            
            cell.kk_itemNameLabel.text = item.kk_name;
            cell.kk_itemPriceLabel.text = [NSString stringWithFormat: @"¥ %.2f", [item.kk_price doubleValue]];
            cell.kk_itemCountLabel.text = [NSString stringWithFormat: @"x %@", item.kk_number];
            
        }
            break;
        case 2:
        {
            cell = [OrderDetailTableViewCell cellFromTableView: tableView index: 2];
            
            double logisticsAmount = [[_orderItem kk_logisticsAmount] doubleValue];
            double itemsAmount = [[_orderItem kk_amount] doubleValue];
            
            cell.kk_logisticsAmountLabel.text = [NSString stringWithFormat: @"¥ %.2f", logisticsAmount];
            cell.kk_commodityAmountLabel.text = [NSString stringWithFormat: @"¥ %.2f", itemsAmount];
            cell.kk_orderAmountLabel.text = [NSString stringWithFormat: @"¥ %.2f", itemsAmount + logisticsAmount];
        }
            break;
            
        default:
            break;
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}



@end
